package com.rill.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author ZhangPeng
 * @date 2010-11-18	
 * 处理时间的类函数
 */
public class DateTimeUtil {
	private static final SimpleDateFormat format_yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
	private static final SimpleDateFormat format_yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss");
	private static final SimpleDateFormat format_yyyy_MM_dd_HH_mm_ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public static String format(String formatStr){
		return format(new Date(),formatStr);
	}
	public static String format(String formatStr,int field,int offset){
		return format(new Date(), formatStr, field,offset);
	}	
	public static String format(Date dateTime,String formatStr){
		return format(dateTime, formatStr,-1, 0);
	}
	/**
	 * 格式化化输出时间
	 * @param dateTime 传入的时间
	 * @param formatStr 格式化类型
	 * @param field 日历类型
	 * @param offset 偏移量
	 * @return
	 */
	public static String format(Date dateTime,String formatStr,int field,int offset){
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateTime);
		if(field != -1){
			cal.add(field,offset);
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat(formatStr);
		return dateFormat.format(cal.getTime());
	}
	public static Date parse(String dateStr,String formatStr) throws ParseException{
		SimpleDateFormat dateFormat = new SimpleDateFormat(formatStr);
		return dateFormat.parse(dateStr);
	}
	public static Date offsetDay(int day){
		return offsetTime(new Date(), 0, 0, day, 0, 0, 0);
	}	
	public static Date offsetMonth(int month){
		return offsetTime(new Date(), 0, month, 0, 0, 0, 0);
	}		
	public static Date offsetDay(Date dateTime,int day){
		return offsetTime(new Date(), 0, 0, day, 0, 0, 0);
	}	
	public static Date offsetMonth(Date dateTime,int month){
		return offsetTime(new Date(), 0, month, 0, 0, 0, 0);
	}		
	public static Date offsetTime(int second){
		return offsetTime(new Date(), 0, 0, 0, 0, 0, second);
	}
	public static Date offsetTime(int minute,int second){
		return offsetTime(new Date(), 0, 0, 0, 0, minute, second);
	}
	public static Date offsetTime(int hour,int minute,int second){
		return offsetTime(new Date(), 0, 0, 0, hour, minute, second);
	}
	public static Date offsetTime(int day,int hour,int minute,int second){
		return offsetTime(new Date(), 0, 0, day, hour, minute, second);
	}
	public static Date offsetTime(int month,int day,int hour,int minute,int second){
		return offsetTime(new Date(), 0, month, day, hour, minute, second);
	}
	public static Date offsetTime(int year,int month,int day,int hour,int minute,int second){
		return offsetTime(new Date(), year, month, day, hour, minute, second);
	}
	public static Date offsetTime(Date dateTime,int second){
		return offsetTime(dateTime, 0, 0, 0, 0, 0, second);
	}
	public static Date offsetTime(Date dateTime,int minute,int second){
		return offsetTime(dateTime, 0, 0, 0, 0, minute, second);
	}
	public static Date offsetTime(Date dateTime,int hour,int minute,int second){
		return offsetTime(dateTime, 0, 0, 0, hour, minute, second);
	}
	public static Date offsetTime(Date dateTime,int day,int hour,int minute,int second){
		return offsetTime(dateTime, 0, 0, day, hour, minute, second);
	}
	public static Date offsetTime(Date dateTime,int month,int day,int hour,int minute,int second){
		return offsetTime(dateTime, 0, month, day, hour, minute, second);
	}
	/**
	 * 时间的偏移运算函数
	 * @param dateTime 传入的时间
	 * @param year 年偏移量
	 * @param month 月偏移量
	 * @param day 日偏移量
	 * @param hour 时偏移量
	 * @param minute 分偏移量
	 * @param second 秒偏移量
	 * @return 偏移后的时间
	 */
	public static Date offsetTime(Date dateTime,int year,int month,int day,int hour,int minute,int second){
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateTime);
		cal.add(Calendar.YEAR,year);
		cal.add(Calendar.MONTH,month);
		cal.add(Calendar.DATE,day);
		cal.add(Calendar.HOUR_OF_DAY,hour);
		cal.add(Calendar.MINUTE,minute);
		cal.add(Calendar.SECOND,second);
		return cal.getTime();
	}
	
	public static Date specifyDay(Date dateTime,int day){
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateTime);
		cal.set(Calendar.DATE,day);
		return cal.getTime();
	} 
	public static Date specifyTime(int second){
		return specifyTime(new Date(), -1, -1, -1, -1, -1, second);
	}
	public static Date specifyTime(int minute,int second){
		return specifyTime(new Date(), -1, -1, -1, -1, minute, second);
	}
	public static Date specifyTime(int hour,int minute,int second){
		return specifyTime(new Date(), -1, -1, -1, hour, minute, second);
	}
	public static Date specifyTime(int day,int hour,int minute,int second){
		return specifyTime(new Date(), -1, -1, day, hour, minute, second);
	}
	public static Date specifyTime(int month,int day,int hour,int minute,int second){
		return specifyTime(new Date(), -1, month, day, hour, minute, second);
	}
	public static Date specifyTime(int year,int month,int day,int hour,int minute,int second){
		return specifyTime(new Date(), year, month, day, hour, minute, second);
	}
	public static Date specifyTime(Date dateTime,int second){
		return specifyTime(dateTime, -1, -1, -1, -1, -1, second);
	}
	public static Date specifyTime(Date dateTime,int minute,int second){
		return specifyTime(dateTime, -1, -1, -1, -1, minute, second);
	}
	public static Date specifyTime(Date dateTime,int hour,int minute,int second){
		return specifyTime(dateTime, -1, -1, -1, hour, minute, second);
	}
	public static Date specifyTime(Date dateTime,int day,int hour,int minute,int second){
		return specifyTime(dateTime, -1, -1, day, hour, minute, second);
	}
	public static Date specifyTime(Date dateTime,int month,int day,int hour,int minute,int second){
		return specifyTime(dateTime, -1, month, day, hour, minute, second);
	}
	/**根据传入的年月日时分秒，指定时间
	 * @param dateTime	传入的时间
	 * @param year	设置的年
	 * @param month	设置的月
	 * @param day	设置的天
	 * @param hour	设置的时
	 * @param minute	设置的分
	 * @param second	设置的秒
	 * @return	设置后的时间
	 */
	public static Date specifyTime(Date dateTime,int year,int month,int day,int hour,int minute,int second){
		Calendar cal = Calendar.getInstance();
		cal.setTime(dateTime);
		if(year != -1){
			cal.set(Calendar.YEAR,year);
		}
		if(month != -1){
			cal.set(Calendar.MONTH,month);
		}
		if(day != -1){
			cal.set(Calendar.DATE,day);
		}
		if(hour != -1){
			cal.set(Calendar.HOUR_OF_DAY,hour);
		}
		if(minute != -1){
			cal.set(Calendar.MINUTE,minute);
		}
		if(second != -1){
			cal.set(Calendar.SECOND,second);
		}
		return cal.getTime();
	}
	/**
	 * 获得指定日期的天开始时间
	 * @param date
	 * @return
	 */
	public static Date getStartTimeOfDay(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY,0);
		cal.set(Calendar.MINUTE,0);
		cal.set(Calendar.SECOND,0);
		date = (Date) cal.getTime();
		return date;
	}
	
	/**
	 * 获得指定日期的天结束时间
	 * @param date
	 * @return
	 */
	public static Date getEndTimeOfDay(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY,23);
		cal.set(Calendar.MINUTE,59);
		cal.set(Calendar.SECOND,59);
		date = (Date) cal.getTime();
		return date;
	}	
	
	/**
	 * 获得指定日期的月开始时间
	 * @param date
	 * @return
	 */
	public static Date getStartTimeOfMonth(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE, 1);
		cal.set(Calendar.HOUR_OF_DAY,0);
		cal.set(Calendar.MINUTE,0);
		cal.set(Calendar.SECOND,0);
		date = (Date) cal.getTime();
		return date;
	}
	
	/**
	 * 获得指定日期的月结束时间
	 * @param date
	 * @return
	 */
	public static Date getEndTimeOfMonth(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DATE, day);
		cal.set(Calendar.HOUR_OF_DAY,23);
		cal.set(Calendar.MINUTE,59);
		cal.set(Calendar.SECOND,59);
		date = (Date) cal.getTime();
		return date;
	}
	
	/**
	 * 返回两个日期相差的天数
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public long getDistDates(Date startDate,Date endDate){
		long totalDate = 0;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(startDate);
		long timestart = calendar.getTimeInMillis();
		calendar.setTime(endDate);
		long timeend = calendar.getTimeInMillis();
		totalDate = Math.abs((timeend - timestart))/(1000*60*60*24);
		return totalDate;
	}
	
	public static void main(String[] args) throws ParseException{
		System.out.println(offsetTime(new Date(),0,0,0,5,0,0));
		System.out.println(getStartTimeOfDay(new Date()));
		System.out.println(getEndTimeOfDay(new Date()));
		System.out.println(getStartTimeOfMonth(new Date()));
		System.out.println(getEndTimeOfMonth(new Date()));
		Date firstDate = DateTimeUtil.parse("2011-02-25 22:01:01", "yyyy-MM-dd HH:mm:ss");
		Date secondDate = new Date();
		System.out.println(firstDate.compareTo(secondDate));
	}
}
